// ========================= 数据类型定义 =========================
/**
 * 地图上散点的数据结构
 * name : 散点名称（如城市名）
 * value : 三元组 [经度, 纬度, 数值]
 *         第 1 位 -> x（经度）
 *         第 2 位 -> y（纬度）
 *         第 3 位 -> value（业务数值，决定散点大小、颜色等）
 */
export interface MapdataType {
    name: string;
    value: [number, number, number] | [number, number];
}

// ========================= 配置生成函数 =========================
/**
 * 根据地区编码及数据生成 ECharts 地图所需的 option
 * @param regionCode  地区编码（如 "china"、"440000"）
 * @param list        地图各区域的数据（用于 visualMap 着色）
 * @param mapData     散点数据（effectScatter 用）
 */
export const optionHandle = (regionCode: string, list: object[], mapData: MapdataType[]) => {
    // 顶部偏移量
    let top = 25;
    const bottom = 25;
    // 缩放级别：如果是中国地图则略大，其余省份默认 1
    let zoom = 1;
    const seriesData = list.map(item => {
        return {
            ...item,
            itemStyle: {
                areaColor: "rgba(24, 45, 166,0.5)",
            },
        };
    });
    return {
        // 背景透明
        backgroundColor: "rgba(0,0,0,0)",
        // 关闭默认 tooltip
        tooltip: { show: false },
        // 关闭图例
        legend: { show: false },
        /* ----------------- visualMap：根据数值分段着色 ----------------- */
        /* ----------------- geo 坐标系（仅做容器，不显示） ----------------- */
        geo: {
            map: regionCode, // 指定地图名称
            roam: false, // 禁止缩放/平移
            selectedMode: false, // 禁止多选
            zoom: zoom,
            top: top,
            show: false, // 本身不渲染，只提供坐标系
        },
        /* ----------------- 系列配置 ----------------- */
        series: [
            /* ===== 第一个系列：填充区域颜色的地图 ===== */
            {
                name: "用户地图",
                type: "map",
                map: regionCode,
                data: seriesData, // 区域数据（用于 visualMap 着色）
                selectedMode: false,
                zoom: zoom,
                top: top,
                bottom: bottom,
                // 区域悬浮提示
                tooltip: {
                    show: true,
                    formatter: (params: any) => (params.data ? `${params.name}：${params.data["value"]}` : params.name),
                    backgroundColor: "rgba(0,0,0,.5)",
                    borderColor: "rgba(147, 235, 248, .8)",
                    textStyle: { color: "#FFF" },
                },

                // 区域标签（默认不显示）
                label: {
                    show: true,
                    color: "#FFF",
                    lineHeight:14,
                    formatter: (val: any) => {
                        return val.data ? ` ${val.name.slice(0, 2)}\n\t${val.data.accountNum} ` : "";
                    },
                },
                // 高亮（hover）样式
                emphasis: {
                    label: {
                        show: true,
                        color: "#FFF",
                        fontSize: 0,
                        position: "insideTop",
                        backgroundColor: {
                            image: "/src/assets/image/data-plus.png",
                        },
                        width: 50,
                        height: 50,
                    },
                    itemStyle: {
                        areaColor: {
                            type: "radial",
                            x: 0.5,
                            y: 0.5,
                            r: 0.8,
                            colorStops: [
                                { offset: 0, color: "#0059FF" },
                                { offset: 1, color: "#0059FF" },
                            ],
                            globalCoord: false,
                        },

                        borderWidth: 1,
                    },
                },
                // 默认样式
                itemStyle: {
                    borderColor: "#FFF",
                    borderWidth: 1,
                    color: "#FFFFFF",
                    shadowColor: "rgba(0, 0, 0, .6)",
                    shadowOffsetX: 0,
                    shadowOffsetY: 8,
                    shadowBlur: 16,
                },
            },
        ],
        // 如有需要可打开动画
        animationDuration: 1000,
        animationEasing: "linear",
        animationDurationUpdate: 1000,
    };
};

// ========================= 行政区划编码对照表 =========================
/**
 * key  : 中文行政区名
 * value: adcode -> 高德/国标行政区编码
 *        level  -> country / province
 *        name   -> 官方全称
 */
export const regionCodes: any = {
    中国: { adcode: "100000", level: "country", name: "中华人民共和国" },
    新疆维吾尔自治区: { adcode: "650000", level: "province", name: "新疆维吾尔自治区" },
    湖北省: { adcode: "420000", level: "province", name: "湖北省" },
    辽宁省: { adcode: "210000", level: "province", name: "辽宁省" },
    广东省: { adcode: "440000", level: "province", name: "广东省" },
    内蒙古自治区: { adcode: "150000", level: "province", name: "内蒙古自治区" },
    黑龙江省: { adcode: "230000", level: "province", name: "黑龙江省" },
    河南省: { adcode: "410000", level: "province", name: "河南省" },
    山东省: { adcode: "370000", level: "province", name: "山东省" },
    陕西省: { adcode: "610000", level: "province", name: "陕西省" },
    贵州省: { adcode: "520000", level: "province", name: "贵州省" },
    上海市: { adcode: "310000", level: "province", name: "上海市" },
    重庆市: { adcode: "500000", level: "province", name: "重庆市" },
    西藏自治区: { adcode: "540000", level: "province", name: "西藏自治区" },
    安徽省: { adcode: "340000", level: "province", name: "安徽省" },
    福建省: { adcode: "350000", level: "province", name: "福建省" },
    湖南省: { adcode: "430000", level: "province", name: "湖南省" },
    海南省: { adcode: "460000", level: "province", name: "海南省" },
    江苏省: { adcode: "320000", level: "province", name: "江苏省" },
    青海省: { adcode: "630000", level: "province", name: "青海省" },
    广西壮族自治区: { adcode: "450000", level: "province", name: "广西壮族自治区" },
    宁夏回族自治区: { adcode: "640000", level: "province", name: "宁夏回族自治区" },
    浙江省: { adcode: "330000", level: "province", name: "浙江省" },
    河北省: { adcode: "130000", level: "province", name: "河北省" },
    香港特别行政区: { adcode: "810000", level: "province", name: "香港特别行政区" },
    台湾省: { adcode: "710000", level: "province", name: "台湾省" },
    澳门特别行政区: { adcode: "820000", level: "province", name: "澳门特别行政区" },
    甘肃省: { adcode: "620000", level: "province", name: "甘肃省" },
    四川省: { adcode: "510000", level: "province", name: "四川省" },
    天津市: { adcode: "120000", level: "province", name: "天津市" },
    江西省: { adcode: "360000", level: "province", name: "江西省" },
    云南省: { adcode: "530000", level: "province", name: "云南省" },
    山西省: { adcode: "140000", level: "province", name: "山西省" },
    北京市: { adcode: "110000", level: "province", name: "北京市" },
    吉林省: { adcode: "220000", level: "province", name: "吉林省" },
};
